package com.gljx.web.service.impl;

import com.gljx.constant.ActivitiKey;
import com.gljx.util.ActitiviUtil;
import com.gljx.util.Axamine;
import com.gljx.util.ServerResponse;
import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.*;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.RecordinMapper;
import com.gljx.web.dao.RecordinentryMapper;
import com.gljx.web.dao.StudentMapper;
import com.gljx.web.service.CustomLogService;
import com.gljx.web.service.RecordinService;
import com.gljx.web.service.RecordinentryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/7 0007.
 */
@Service
public class RecordinServiceImpl extends CommonDao implements RecordinService {

    @Autowired
    RecordinentryService recordinentryService;

    @Autowired
    RecordinMapper recordinMapper;

    @Autowired
    RecordinentryMapper recordinentryMapper;

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    CustomLogService customLogService;

    @Autowired
    HttpSession session;

    @Override
    public Map<String, Object> selectPageCount(String psql) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Psql", psql);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        this.getSqlSession().select("com.gljx.web.dao.RecordinMapper.pagecount", paramMap, null);
        return paramMap;
    }

    @Override
    public Map<String, Object> selectPage(int pindex, String psql, int psize) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Pindex", pindex);
        paramMap.put("Psql", psql);
        paramMap.put("Psize", psize);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
        this.getSqlSession().select("com.gljx.web.dao.RecordinMapper.pagemap", paramMap, null);
        return paramMap;
    }

    /**
     * @Author lcx
     * @Date 2017/11/8 0008 下午 2:38
     * 根据fid查询档案
     */
    @Override
    public Recordin selectRecordinByFid(String fid) {

        Recordin recordin = recordinMapper.selectByPrimaryKey(fid);
        List<Recordinentry> recordinentryList = recordinentryService.selectRecordinentryByFid(fid, "");
        recordin.setRecordinentryList(recordinentryList);

        return recordin;
    }

    /**
     * @Author lcx
     * @Date 2017/11/9 0009 下午 4:08
     * 更新数据
     */
    @Override
    public void updateRecordin(Recordin recordin) {
        recordinMapper.updateByPrimaryKeySelective(recordin);
    }

    @Override
    public void addRecordin(Recordin recordin) {
        recordinMapper.insertSelective(recordin);
    }

    /**
     * @Author lcx
     * @Date 2017/11/13 0013 下午 12:28
     * 删除父类以及子类
     */
    @Override
    public void deleteByRecordinKey(String[] deleteId, String[] fnumber) {
        ActitiviUtil actitiviUtil = new ActitiviUtil();
        //删除记录
        for (int i = 0; i < deleteId.length; i++) {
            /*过滤为空的删除操作*/
            if (!deleteId[i].equals("")) {
                actitiviUtil.deleteTask(ActivitiKey.PUBLIC_KEY, fnumber[i]);
                recordinMapper.deleteByPrimaryKey(deleteId[i]);
                recordinentryMapper.deleteByFparentid(deleteId[i]);
            }
        }
    }

    @Override
    public void deleteRecordinAndRecordintentry(String fid) {
        recordinMapper.deleteByPrimaryKey(fid);
        recordinentryMapper.deleteByFparentid(fid);
    }

    @Override
    public ServerResponse examine(Recordin recordin, String state, String agreed, String auditcomment,
                                  String[] rcfcfwz, String[] rcfjyzbh, String[] rcfremark) {
        //如果是审核
        Axamine axamine = new Axamine();
        List<Recordinentry> recordinentryList = recordinentryMapper.selectRecordinentryByFparentid(recordin.getFid(), "");
        if ("5".equalsIgnoreCase(state)) {
            ServerResponse serverResponse = axamine.auditProcess(recordin.getFnumber(), agreed, auditcomment);
            //如果返回 1 代表 流程结束 2代表流程还在继续 0 代表错误
            if (serverResponse.getStatus() == 1) {
                recordin.setCfbillstate(Long.valueOf("5"));
                //修改学生的状态为 已结业
                //获取操作人信息
                User user = (User) session.getAttribute("user");

                //获取当前修改时间
                String createTime = TimeFormat.timeFormat(new Date());
                //修改时间
                recordin.setFlastupdatetime(createTime);
                //修改人id
                recordin.setFauditorid(user.getFid());

                //添加学生的服务日志
                for (Recordinentry recordinentry : recordinentryList) {
                    Student student = new Student();
                    student.setFid(recordinentry.getCfstudentid());
                    student.setCfstudentstate(Long.valueOf("10"));
                    student.setCfdazt("入库");
                    student.setCfcfwz(recordinentry.getCfcfwz());
                    student.setCfnation(recordinentry.getCfjyzbh());
                    student.setCfremark(recordinentry.getCfremark());
                    studentMapper.updateByPrimaryKeySelective(student);

                    customLogService.addLog("档案入库审核", TimeFormat.timeFormat(new Date()) + "档案入库审核",
                            recordinentry.getCfstudentid(), ((User) session.getAttribute("user")).getFid());
                }

                recordinMapper.updateByPrimaryKeySelective(recordin);
            }
            //如果是不同意状态则修改单据状态为保存
            if ("2".equals(agreed)) {
                recordin.setCfbillstate(Long.valueOf("3"));
                recordinMapper.updateByPrimaryKeySelective(recordin);
            }


            return serverResponse;
        }
        if ("3".equalsIgnoreCase(state)) {
            recordin.setCfbillstate(Long.valueOf("3"));
            recordin.setFsourcefunction(auditcomment);
            axamine.againstAuditProcess(recordin.getFnumber(), auditcomment);
            recordinMapper.updateByPrimaryKeySelective(recordin);

            //添加学生的服务日志
            for (Recordinentry recordinentry : recordinentryList) {
                customLogService.addLog("档案入库反审核", TimeFormat.timeFormat(new Date()) + "档案入库反审核",
                        recordinentry.getCfstudentid(), ((User) session.getAttribute("user")).getFid());
            }
            return ServerResponse.createBySuccessMessage("反审核成功！");
        }

        return ServerResponse.createByError();
    }

    @Override
    public List<Recordinentry> selectRecordinXls(String fnumber, String name, String beginTime, String endTime, String fcompanyid) {
        return recordinMapper.selectRecordinXls(fnumber, name, beginTime, endTime, fcompanyid);
    }
}
